#include "stdafx.h"

#include "LookFeelProperty.h"
#include "AllLookFeel.h"
#include "LookFeelEditor.h"
#include "App.h"


LookFeelProperty::LookFeelProperty()
{
	mLayout = new MGUI_Layout(AllLookFeel::Instance()->GetPanel());

	int top = 0;
	const int K_Space = 8;

	// Name
	//
	MGUI_Label * lb_Name = new MGUI_Label(NULL, mLayout);
	lb_Name->SetCaption(L"Name:");
	lb_Name->SetRect(MGUI_Rect(true, 0, top , 64, 24));

	mEditBox_Name = new MGUI_EditBox(AllLookFeel::Instance()->GetEditBox(), mLayout);
	mEditBox_Name->SetCaption(L"");
	mEditBox_Name->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// Skin
	//
	MGUI_Label * lb_Skin = new MGUI_Label(NULL, mLayout);
	lb_Skin->SetCaption(L"Skin:");
	lb_Skin->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mEditBox_Skin = new MGUI_EditBox(AllLookFeel::Instance()->GetEditBox(), mLayout);
	mEditBox_Skin->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// Client
	//
	MGUI_Label * lb_Client = new MGUI_Label(NULL, mLayout);
	lb_Client->SetCaption(L"Client:");
	lb_Client->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mEditBox_Client = new MGUI_EditBox(AllLookFeel::Instance()->GetEditBox(), mLayout);
	mEditBox_Client->SetCaption(L"0 0 1 1");
	mEditBox_Client->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// State
	//
	MGUI_Label * lb_State = new MGUI_Label(NULL, mLayout);
	lb_State->SetCaption(L"State:");
	lb_State->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mComboBox_State = new MGUI_ComboBox(AllLookFeel::Instance()->GetComboBox(), mLayout);
	mComboBox_State->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// UV Rect
	//
	MGUI_Label * lb_UVRect = new MGUI_Label(NULL, mLayout);
	lb_UVRect->SetCaption(L"UVRect:");
	lb_UVRect->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mEditBox_UVRect = new MGUI_EditBox(AllLookFeel::Instance()->GetEditBox(), mLayout);
	mEditBox_UVRect->SetCaption(L"0 0 1 1");
	mEditBox_UVRect->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// TextColor
	//
	MGUI_Label * lb_TextColor = new MGUI_Label(NULL, mLayout);
	lb_TextColor->SetCaption(L"TColor:");
	lb_TextColor->SetRect(MGUI_Rect(true, 0, top, 64, 24));

	mWidget_TextColor = new MGUI_Widget(AllLookFeel::Instance()->GetWhite(), mLayout);
	mWidget_TextColor->SetRect(MGUI_Rect(true, 80, top, 165, 24));

	top += 24 + K_Space;

	// Init
	mEditBox_Skin->SetCaption(L"");

	for (int i = 0; i < MGUI_WidgetState::Max; ++i)
		mComboBox_State->Append(MGUI_WidgetState::GetStateString(i).c_wstr());

	mComboBox_State->Append(L"All");

	mComboBox_State->SetSelectIndex(0);

	OnEditLookFeelChanged_(NULL);

	LookFeelEditor::Instance()->eventEditLookFeelChanged += OnEditLookFeelChanged(this, &LookFeelProperty::OnEditLookFeelChanged_);
	LookFeelEditor::Instance()->eventEditRectChanged += OnEditRectChanged(this, &LookFeelProperty::OnEditRectChanged_);

	mEditBox_Skin->eventKeyLostFocus += OnSkinChanged(this, &LookFeelProperty::OnSkinChanged_);
	mComboBox_State->eventSelectChanged += OnStateChanged(this, &LookFeelProperty::OnStateChanged_);

	mEditBox_Name->eventKeyLostFocus += OnNameReturn(this, &LookFeelProperty::OnNameReturn_);
	mEditBox_Client->eventKeyLostFocus += OnClientReturn(this, &LookFeelProperty::OnClientReturn_);
	mEditBox_UVRect->eventKeyLostFocus += OnUVRectReturn(this, &LookFeelProperty::OnUVRectReturn_);
}

LookFeelProperty::~LookFeelProperty()
{
	delete mLayout;
}

void LookFeelProperty::Layout()
{
	MGUI_Rect rect;

	rect.x0 = MGUI_Engine::Instance()->GetRect().x1 - 256;
	rect.x1 = MGUI_Engine::Instance()->GetRect().x1;

	rect.y0 = 32;
	rect.y1 = MGUI_Engine::Instance()->GetRect().y1 - 32;

	mLayout->SetRect(rect);
}

void LookFeelProperty::OnEditLookFeelChanged_(MGUI_LookFeel * _lookfeel)
{
	if (_lookfeel)
	{
		int state = MGUI_WidgetState::Normal;

		mEditBox_Name->SetCaption(_lookfeel->GetName().c_wstr());
		mEditBox_Client->SetCaption(_lookfeel->GetClientRect().ToString().c_wstr());

		mEditBox_UVRect->SetCaption(_lookfeel->GetUVRect(state).ToString().c_wstr());

		if (_lookfeel->GetSkin() != NULL)
		{
			mEditBox_Skin->SetCaption(_lookfeel->GetSkin()->GetName().c_wstr());
			mComboBox_State->SetSelectIndex(state);
		}
		else
		{
			mEditBox_Skin->SetCaption(L"");
		}
	}
	else
	{
		mEditBox_Name->SetCaption(L"");
		mEditBox_Client->SetCaption(MGUI_ClientRect().ToString().c_wstr());

		mEditBox_UVRect->SetCaption(MGUI_Rect().ToString().c_wstr());
		mWidget_TextColor->SetColor(MGUI_Color::Black);

		mEditBox_Skin->SetCaption(L"");
	}

	OnUVRectReturn_(NULL);
}

void LookFeelProperty::OnEditRectChanged_(MGUI_Rect _rect)
{
	mEditBox_UVRect->SetCaption(_rect.ToString().c_wstr());

	MGUI_LookFeel * lookfeel = LookFeelEditor::Instance()->GetEditLookFeel();

	if (lookfeel == NULL)
		return ;

	int state = mComboBox_State->GetSelectIndex();

	if (state == -1)
		return ;

	if (state == MGUI_WidgetState::Max)
	{
		for (int i = 0; i < MGUI_WidgetState::Max; ++i)
		{
			lookfeel->SetUVRect(i, _rect);
		}
	}
	else
	{
		lookfeel->SetUVRect(state, _rect);
	}
}

void LookFeelProperty::OnSkinChanged_(MGUI_Widget * _widget)
{
	TString128 skin = TString128::FromUnicode(mEditBox_Skin->GetCaption().c_str());

	if (skin != "")
	{
		TexturePtr p = TextureManager::Instance()->LoadTexture(skin, skin);
			
		LookFeelEditor::Instance()->_notifySkinChanged(p.c_ptr());
	}
	else
	{
		LookFeelEditor::Instance()->_notifySkinChanged(NULL);
	}
}

void LookFeelProperty::OnStateChanged_(int _index)
{
	if (_index == -1)
		return ;

	MGUI_LookFeel * lookfeel = LookFeelEditor::Instance()->GetEditLookFeel();

	if (lookfeel == NULL)
		return ;

	if ( _index == MGUI_WidgetState::Max)
		_index = MGUI_WidgetState::Normal;

	mEditBox_UVRect->SetCaption(lookfeel->GetUVRect(_index).ToString().c_wstr());

	OnUVRectReturn_(NULL);
}

void LookFeelProperty::OnNameReturn_(MGUI_Widget * _widget)
{
	MGUI_LookFeel * lookfeel = LookFeelEditor::Instance()->GetEditLookFeel();

	if (lookfeel == NULL)
		return ;

	TString128 name = TString128::FromUnicode(mEditBox_Name->GetCaption().c_str());

	if (name == lookfeel->GetName())
		return ;

	if (name == "")
	{
		MessageBox(App::Instance()->_hWnd(), "look feel name can't be empty!", "Error", MB_ICONERROR | MB_OK);
	}
	else if (MGUI_LookFeelManager::Instance()->GetLookFeel(LookFeelEditor::Instance()->GetEditWidgetType(), name) != NULL)
	{
		MessageBox(App::Instance()->_hWnd(), "look feel exist!", "Error", MB_ICONERROR | MB_OK);
	}
	else
	{
		lookfeel->SetName(name);
		LookFeelEditor::Instance()->_notifyNameChanged();
	}

	mEditBox_Name->SetCaption(lookfeel->GetName().c_wstr());
}

void LookFeelProperty::OnClientReturn_(MGUI_Widget * _widget)
{
	const MGUI_String & text = mEditBox_Client->GetCaption();
	TString128 strRect = TString128::FromUnicode(text.c_str());

	MGUI_ClientRect rect;

	rect.FromString(strRect);

	LookFeelEditor::Instance()->_notifyClientChanged(rect);
}

void LookFeelProperty::OnUVRectReturn_(MGUI_Widget * _widget)
{
	const MGUI_String & text = mEditBox_UVRect->GetCaption();
	TString128 strRect = TString128::FromUnicode(text.c_str());

	MGUI_Rect rect;

	rect.FromString(strRect);

	LookFeelEditor::Instance()->_notifyRectChanged(rect);
}

